package com.ruoyi.mall.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.domain.ProductCategoryDO;
import com.ruoyi.mall.service.IProductCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类Controller
 *
 * @author ruoyi
 * @date 2025-03-28
 */
@Tag(name = "商品分类管理")
@RestController
@RequestMapping("/mall/category")
public class ProductCategoryController extends BaseController
{
    @Autowired
    private IProductCategoryService productCategoryService;

    /**
     * 查询商品分类列表
     */
    @Operation(summary = "查询商品分类列表")
    @PreAuthorize("@ss.hasPermi('mall:category:list')")
    @GetMapping("/list")
    public AjaxResult list(ProductCategoryDO productCategory)
    {
        List<ProductCategoryDO> list = productCategoryService.selectProductCategoryList(productCategory);
        return success(list);
    }

    /**
     * 导出商品分类列表
     */
    @Operation(summary = "导出商品分类列表")
    @PreAuthorize("@ss.hasPermi('mall:category:export')")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductCategoryDO productCategory)
    {
        List<ProductCategoryDO> list = productCategoryService.selectProductCategoryList(productCategory);
        ExcelUtil<ProductCategoryDO> util = new ExcelUtil<ProductCategoryDO>(ProductCategoryDO.class);
        util.exportExcel(response, list, "商品分类数据");
    }

    /**
     * 获取商品分类详细信息
     */
    @Operation(summary = "获取商品分类详细信息")
    @PreAuthorize("@ss.hasPermi('mall:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productCategoryService.getById(id));
    }

    /**
     * 新增商品分类
     */
    @Operation(summary = "新增商品分类")
    @PreAuthorize("@ss.hasPermi('mall:category:add')")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductCategoryDO productCategory)
    {
        return toAjax(productCategoryService.insertProductCategory(productCategory));
    }

    /**
     * 修改商品分类
     */
    @Operation(summary = "修改商品分类")
    @PreAuthorize("@ss.hasPermi('mall:category:edit')")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductCategoryDO productCategory)
    {
        return toAjax(productCategoryService.updateProductCategory(productCategory));
    }

    /**
     * 删除商品分类
     */
    @Operation(summary = "删除商品分类")
    @PreAuthorize("@ss.hasPermi('mall:category:remove')")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productCategoryService.deleteProductCategoryByIds(ids));
    }
}
